﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace FileBrowser.Model
{
    class IDataUtils
    {
        internal class FileExtensions
        {
            public static readonly string[] Document = new string[] { ".doc", ".xls", ".ppt", ".docx", ".xlsx", ".pptx", ".pdf", ".txt", ".rtf" };
            public static readonly string[] Image = new string[] { ".jpg", ".png", ".bmp", ".gif", ".tif" };
            public static readonly string[] Music = new string[] { ".mp3", ".wma", ".m4a", ".aac" };
        }
        public static async Task<ImageSource> getIconFromFile(string name)
        {
            
            string[] strs = name.Split('.');
            string ext = strs[strs.Length - 1];
            StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile f = await storageFolder.CreateFileAsync("dummy." + ext, CreationCollisionOption.OpenIfExists);
            
            ThumbnailMode tmode = ThumbnailMode.SingleItem;
            if (FileExtensions.Image.Contains("." + ext))
                tmode = ThumbnailMode.PicturesView;
            else if (FileExtensions.Document.Contains("." + ext))
                tmode = ThumbnailMode.DocumentsView;
            else if (FileExtensions.Music.Contains("." + ext))
                tmode = ThumbnailMode.MusicView;
            using (StorageItemThumbnail thumbnail = await f.GetThumbnailAsync(tmode, 200, ThumbnailOptions.UseCurrentScale))
            {
                if (thumbnail != null)
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(thumbnail);
                    return bitmapImage;   
                }                                
            }
            //Debug.WriteLine(ext);
            StorageItemThumbnail thumbnail2= await f.GetThumbnailAsync(ThumbnailMode.SingleItem,50,ThumbnailOptions.UseCurrentScale);
            BitmapImage bitmapImage2 = new BitmapImage();
            bitmapImage2.SetSource(thumbnail2);
            return bitmapImage2;
            
        }
        public static ImageSource getIconFromFolder(string name)
        {
            return null;
        }
        private static async Task<StorageFile> createDummyFile(string name)
        {
            string[] strs = name.Split('.');
            string ext = strs[strs.Length - 1];
            StorageFolder storageFolder =Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile dummyFile = await storageFolder.CreateFileAsync("dummy."+ext, CreationCollisionOption.OpenIfExists);
            return dummyFile;
            //rootPage.sampleFile = await storageFolder.CreateFileAsync(MainPage.filename, CreationCollisionOption.ReplaceExisting);
            //OutputTextBlock.Text = "The file '" + rootPage.sampleFile.Name + "' was created.";
        }
        /// <summary>
        /// grouping data
        /// </summary>
        /// <param name="idata"></param>
        /// <returns></returns>
        public static ObservableCollection<IDataGrouped<IData>> createGroupIData(List<IData> idata)
        {
            ObservableCollection<IDataGrouped<IData>> groups = new ObservableCollection<IDataGrouped<IData>>();
            var query = from i in idata
                        orderby i.getID()
                        group i by i.getType() into grp
                        select new { GroupName = grp.Key, Items = grp };
            foreach (var g in query)
            {
                IDataGrouped<IData> group = new IDataGrouped<IData>
                {
                    Key = g.GroupName.ToString()
                    
                };
                Debug.WriteLine("group name " + g.GroupName);
                foreach (IData id in g.Items)
                {
                    group.Add(id);
                }
                groups.Add(group);
            }
            return groups;
            // sample:::
            /*
            ObservableCollection<IDataGrouped<IData>> result = new ObservableCollection<IDataGrouped<IData>>();
            IDataGrouped<IData> files = new IDataGrouped<IData>() { Key = "Files"};
            foreach (IData id in idata)
            {
                if (id.getType() == IData.Type.File)
                {
                    id.customType = 0;
                    files.Add(id);
                }
                else
                {
                    IDataGrouped<IData> group = new IDataGrouped<IData>() { Key = id.Name };
                    int i = 1;
                    id.customType = 1;
                    group.Add(id);
                    foreach (IData idd in id.getItems())
                    {
                        if (idd.getType() == IData.Type.File)
                        {
                            i++;
                            group.Add(idd);
                            idd.customType = 1;
                            if (i == 4) break; // maximal 3
                        }
                    }
                    result.Add(group);
                }
            }
            result.Add(files);
            return result;
            */
        }
    }
}

